import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:schoollifedemo/business/chat/chat_home_page.dart';
import 'package:schoollifedemo/business/home/page/edit_friend_circle_page.dart';
import 'package:schoollifedemo/business/login/register_page.dart';
import 'package:schoollifedemo/business/main_page.dart';
import 'package:schoollifedemo/business/personal/page/password_change_page.dart';
import 'package:schoollifedemo/business/personal/page/personal_friend_circle_edit_page.dart';
import 'package:schoollifedemo/business/personal/page/personal_home_page.dart';
import 'package:schoollifedemo/business/login/login_page.dart';
import 'package:schoollifedemo/business/personal/page/personal_icon_select_page.dart';
import 'package:schoollifedemo/business/personal/page/personal_info_edit_page.dart';
import 'package:schoollifedemo/business/personal/page/settings_page.dart';
import 'package:schoollifedemo/business/personal/page/software_problem_submit_page.dart';
import 'package:schoollifedemo/business/school/page/announcement_detail_page.dart';
import 'package:schoollifedemo/business/school/page/announcement_see_more_page.dart';
import 'package:schoollifedemo/business/school/page/memorandum_edit_page.dart';
import 'package:schoollifedemo/business/school/page/memorandum_select_all_page.dart';
import 'package:schoollifedemo/business/welcome/privacy_page.dart';

// define router handler, one page one handler
// 聊天页面
final _chatHomeHandler = Handler(handlerFunc: (context, params) {
  return const ChatHomePage();
});

// 个人主页
final _personalHomeHandler = Handler(handlerFunc: (context, params) {
  return const PersonalPage();
});

// 隐私页面
final _privacyPageHandler = Handler(handlerFunc: (context, params) {
  return const PrivacyPage();
});

// 登录页面
final _loginPageHandler = Handler(handlerFunc: (context, params) {
  return const LoginPage();
});

// 注册页面
final _registerPageHandler = Handler(handlerFunc: (context, params) {
  return const RegisterPage();
});

// 主要页面
final _mainPageHandler = Handler(handlerFunc: (context, params) {
  return const MainPage();
});

// 编辑页面
final _editFriendCirclePage = Handler(handlerFunc: (context, params) {
  return const EditFriendCirclePage();
});

final _settingsPage = Handler(handlerFunc: (context, params) {
  return const SettingsPage();
});

final _passwordChangePage = Handler(handlerFunc: (context, params) {
  return const PasswordChangePage();
});

final _softwareProblemSubmitPage = Handler(handlerFunc: (context, params) {
  return const SoftWareProblemSubmitPage();
});
final _editPersonalInfoPage = Handler(handlerFunc: (context, params) {
  return const PersonalInfoEditPage();
});

final _editPersonalFriendCircleListPage =
    Handler(handlerFunc: (context, params) {
  return const PersonalFriendCircleEditPage();
});

final _selectPersonalIconPage = Handler(handlerFunc: (context, params) {
  return const PersonalIconSelectPage();
});

final _selectAllMemorandumPage = Handler(handlerFunc: (context, params) {
  return const SelectAllMemorandumPage();
});

final _editMemorandumPage = Handler(handlerFunc: (context, params) {
  final noteId = params['note_id']?[0];
  final isEditMode = params['is_edit_mode']?[0];
  return EditMemorandumPage(
    noteId: noteId,
    isEditMode: isEditMode == '1',
  );
});

final _seeMoreAnnouncementPage = Handler(handlerFunc: (context, params) {
  final mainTitleMd5 = params['main_title_md5']?[0];
  return AnnouncementSeeMorePage(
    mainTitleMd5: mainTitleMd5 ?? '',
  );
});

final _seeSingleAnnouncementDetailPage =
    Handler(handlerFunc: (context, params) {
  String? url = params['base64_url']?[0];
  String? mainTitle = params['base64_main_title']?[0];
  return AnnouncementDetailPage(
    base64Url: url,
    base64MainTitle: mainTitle,
  );
});

// 路由注册
void defineRoutes(FluroRouter? router) {
  router?.define(
    RoutersPath.chatHomePage,
    handler: _chatHomeHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.personalHomePage,
    handler: _personalHomeHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.privacyPage,
    handler: _privacyPageHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.loginPage,
    handler: _loginPageHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.registrationPage,
    handler: _registerPageHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.homePage,
    handler: _mainPageHandler,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.editFriendCirclePage,
    handler: _editFriendCirclePage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.settingsPage,
    handler: _settingsPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.passwordChangePage,
    handler: _passwordChangePage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.softwareProblemSubmitPage,
    handler: _softwareProblemSubmitPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.editPersonalInfoPage,
    handler: _editPersonalInfoPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.editPersonalFriendCircleListPage,
    handler: _editPersonalFriendCircleListPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.selectPersonalIconPage,
    handler: _selectPersonalIconPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.selectAllMemorandumPage,
    handler: _selectAllMemorandumPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.editMemorandumPage,
    handler: _editMemorandumPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.seeMoreAnnouncementPage,
    handler: _seeMoreAnnouncementPage,
    transitionType: TransitionType.fadeIn,
  );
  router?.define(
    RoutersPath.seeSingleAnnouncementDetailPage,
    handler: _seeSingleAnnouncementDetailPage,
    transitionType: TransitionType.fadeIn,
  );
}

// 路由路径
class RoutersPath {
  static const chatHomePage = '/chatHomePage'; // 聊天页面
  static const personalHomePage = '/personalHomePage'; //个人主页
  static const privacyPage = '/privacyPage'; // 隐私页面
  static const loginPage = '/loginPage'; // 登录页面
  static const homePage = '/homePage'; // 主页

  /// 朋友圈编辑页面
  static const editFriendCirclePage = '/editFriendCirclePage'; //编辑朋友圈页面
  static const editPersonalFriendCircleListPage =
      '/editPersonalFriendCircleListPage'; // 编辑朋友圈列表界面

  /// 设置界面
  static const settingsPage = '/settingsPage'; // 设置页面
  static const passwordChangePage = '/passwordChangePage'; // 密码修改界面
  static const teacherCertificationPage = '/teacherCertificationPage'; // 教师认证界面
  static const softwareProblemSubmitPage =
      '/softwareProblemSubmitPage'; // 软件问题提交界面

  /// 个人信息编辑界面
  static const registrationPage = '/registrationPage'; // 注册页面
  static const editPersonalInfoPage = '/editPersonalInfoPage'; // 编辑个人信息界面
  static const selectPersonalIconPage = '/selectPersonalIconPage'; // 更改个人头像页面

  /// 备忘录界面
  static const selectAllMemorandumPage = '/allMemorandumPage'; // 查看所有备忘录页面
  static const editMemorandumPage = '/editMemorandumPage'; // 编辑/添加备忘录页面

  /// 学校公告页面
  static const seeMoreAnnouncementPage = '/seeMoreAnnouncementPage'; // 查看公告页面
  static const seeSingleAnnouncementDetailPage =
      '/seeSingleAnnouncementDetailPage'; // 查看单个公告详情页面
}
